package com.kj.mynote.Activity;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kj.mynote.R;
import com.kj.mynote.Util.AutoUtils;
import com.kj.mynote.Util.ToolUtils;
import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.CalendarMode;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnDateSelectedListener;

import java.util.Calendar;
import java.util.Date;

/**
 * @author wy
 * @version V1.0
 * @Title: TimeNote
 * @Package com.kj.mynote.Activity
 * @Description: TODO(用一句话描述该文件做什么)
 * @date 17/3/25 14:14
 */

public class DateSelectActivity extends BaseActivity {
    MaterialCalendarView calendarView;
    RelativeLayout out;
    TextView tvCancel, tvClear, tvOk;
    long sDate;
    long eDate;
    Date selcetDate = new Date();
    int code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dia_date_select);
        AutoUtils.auto(this);
        Intent intent = getIntent();
        code = intent.getIntExtra("code", -1);
        sDate = intent.getLongExtra("sDate", -1);
        eDate = intent.getLongExtra("eDate", -1);
        init();
    }

    private void init() {
        calendarView = (MaterialCalendarView) findViewById(R.id.calendarView);
        calendarView.state().edit().setFirstDayOfWeek(Calendar.SUNDAY).commit();
        switch (code) {
            case 1:
                if (sDate > 0) {
                    calendarView.setSelectedDate(new Date(sDate));
                } else {
                    calendarView.setSelectedDate(new Date());
                }
                break;
            case 2:
                if (eDate > 0) {
                    calendarView.setSelectedDate(new Date(eDate));
                } else {
                    calendarView.setSelectedDate(new Date());
                }
                break;
        }

        calendarView.setOnDateChangedListener(new OnDateSelectedListener() {
            @Override
            public void onDateSelected(@NonNull MaterialCalendarView widget, @NonNull CalendarDay date, boolean selected) {
                selcetDate = date.getDate();
            }
        });
        out = (RelativeLayout) findViewById(R.id.date_rl_out);
        out.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvCancel = (TextView) findViewById(R.id.date_tv_cancel);
        tvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvClear = (TextView) findViewById(R.id.date_tv_clear);
        tvClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("date", 0);
                setResult(code, intent);
                finish();
            }
        });
        tvOk = (TextView) findViewById(R.id.date_tv_OK);
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selcetDate == null) {
                    Toast.makeText(DateSelectActivity.this, "请选择在日期", Toast.LENGTH_SHORT).show();
                    return;
                }
                selcetDate = new Date(ToolUtils.S2TS(ToolUtils.TS2S(selcetDate.getTime(),"yyyy-MM-dd"),"yyyy-MM-dd"));
                Intent intent = new Intent();
                switch (code) {
                    case 1:
                        if (eDate > 0) {
                            if (selcetDate.getTime() > eDate) {
                                Toast.makeText(DateSelectActivity.this, "请选择在结束日期之前", Toast.LENGTH_SHORT).show();
                                return;
                            }
                        }
                        intent.putExtra("date", selcetDate.getTime());
                        setResult(code, intent);
                        finish();
                        break;
                    case 2:
                        if (sDate > 0) {
                            if (selcetDate.getTime() < sDate) {
                                Toast.makeText(DateSelectActivity.this, "请选择在开始日期之后", Toast.LENGTH_SHORT).show();
                                return;
                            }
                        }
                        intent.putExtra("date", selcetDate.getTime());
                        setResult(code, intent);
                        finish();
                        break;
                }
            }
        });
    }
}